<?php
	session_start();
	include("checkTemplate.php");
	include("fetchData.php");
	
	if($clean){
		// Update template table
		modifytemplate($_GET['template_id'],$_POST['template']);
	
		 // Delete all major and/or minor requirements associated with template under modification
		$que = "DELETE minor.*, major.* FROM template LEFT OUTER JOIN major ON template.template_id = major.template_id LEFT OUTER JOIN minor ON major.major_id = minor.major_id WHERE template.template_id = '".$_GET['template_id']."'";
		$result = mysql_query($que);
		if(!$result){
			die('Error :' . mysql_error());
		}
		
		// Add major and/or minor requirements currently on form
		for($i=0;;$i++){
			if($_POST['majName'.$i]==NULL)
				break;
			if($_POST['majName'.$i]!="~NULL")
				createmajor($_GET['template_id'],$_POST['majName'.$i],$_POST['majPer'.$i]);

			// Insert all minReq associated with current majReq
			$data = fetchMostRecentMajorId($_GET['template_id']);
			for($j=0;;$j++){
				if($_POST['minName'.$i.$j]==NULL)
					break;
				if($_POST['minName'.$i.$j]!="~NULL")
					createminor($data['major_id'],$_POST['minName'.$i.$j],$_POST['minSco'.$i.$j]);
			}
		}
		mysql_close();
?>
	<head><title>GradeGrid - Template Modification Success</title></head>
	<body>
		<div style=\"position:absolute; top:-60px; margin-top:50%; left:-183px; margin-left:50%;\">
		<h3 align="center">Your template had been successfully modified!<br/>What would you like to do?</h3>
            <div align="center">
            <a href="addTemplate.php">Add a new template</a>
             OR 
            <a id="close" onClick="window.close()" style="text-decoration:underline; color:#03F" onMouseOver="document.getElementById('close').style.cursor='pointer'">Close this window</a>
			</div>
		</div>
		<div style="font-size:9px; position:absolute; bottom:10px; left:-40px; margin-left:50%;" align="center">GradeGrid Copyright</div>
	</body>
<?php } else { ?>
	<!-- Populate hidden form then pass to addTemplate -->
    	
        <form action="editTemplate.php?retry=1&template_id=<?php echo $_GET['template_id'];?>" id="dummy" style="display:none" method="post">
        <?php
			// Insert to template name to dummy
			echo "<input type=\"text\" name=\"template\" value=\"".$_POST['template']."\">";
				
			// Insert all majReq into dummy
			$k=0;
			for($i=0;;$i++){		
				if($_POST['majName'.$i]==NULL)
					break;
				if($_POST['majName'.$i]!="~NULL"){	
					echo "<input type=\"text\" name=\"majName".$k."\" value=\"".$_POST['majName'.$i]."\">";
					echo "<input type=\"text\" name=\"majPer".$k."\" value=\"".$_POST['majPer'.$i]."\">";
					$l=0;
					// Insert all minReq associated with current majReq
					for($j=0;;$j++){
						if($_POST['minName'.$i.$j]==NULL)
							break;
						if($_POST['minName'.$i.$j]!="~NULL"){
							echo "<input type=\"text\" name=\"minName".$k.$l."\" value=\"".$_POST['minName'.$i.$j]."\">";
							echo "<input type=\"text\" name=\"minSco".$k.$l."\" value=\"".$_POST['minSco'.$i.$j]."\">";
							$l++;
						}
					}
					$k++;
				}
			}
		?>
        </form>
<?php
		echo $error;
		echo "<br/><a id=\"close\" onClick=\"document.getElementById('dummy').submit()\" style=\"text-decoration:underline; color:#03F\" onMouseOver=\"document.getElementById('close').style.cursor='pointer'\">Click here to return to your form</a></div>";
	}
?>